package org.bluedream.core.module.sys.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bluedream.core.module.sys.entity.Organization;
import org.bluedream.core.module.sys.entity.Menu;
import org.bluedream.core.module.sys.entity.Role;
import org.bluedream.core.module.sys.entity.User;

import java.util.List;

public interface RoleDao extends BaseMapper<Role> {
    @Select("select c.* from sys_org_relation a, sys_role b , sys_organization c" +
            " where b.id = #{id} and b.id = a.entity_Id and a.type = 'role'" +
            " and a.org_Id = c.id" +
            " and c.status = '0' and a.status = '0' and b.status = '0'" +
            "order by c.tree_sorts , c.tree_sort")
    List<Organization> getOrganizations(Role role);

    @Select("select c.id as id from sys_org_relation a, sys_role b , sys_organization c" +
            " where b.id = #{id} and b.id = a.entity_Id and a.type = 'role'" +
            " and a.org_Id = c.id" +
            " and c.status = '0' and a.status = '0' and b.status = '0'" +
            "order by c.tree_sorts , c.tree_sort")
    List<String> getOrganizations4Id(Role role);

/*    @Select("select c.* from SYS_ROlE_RELATION a , sys_role b , sys_menu c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            "and a.role_ID = b.id and a.entity_id = c.id and a.type = 'menu' " +
            "and b.id = #{id} " +
            "order by c.tree_sorts , c.tree_sort")
    List<Menu> getMenus(Role role);*/

/*    @Select("select menu.menu_name as treeName , menu.* from sys_menu menu , sys_role_relation roleMenu , " +
            "        (select role.id as id  from sys_role role , sys_org_relation orgRelation , sys_organization org , sys_role_relation roleUser " +
            "            where orgRelation.status = '0' and org.status = '0' and role.status = '0' and roleUser.status = '0' " +
            "             and orgRelation.org_id = org.id and orgRelation.type = 'role' and orgRelation.entity_id = role.id " +
            "             and role.id = roleUser.role_id and roleUser.type = 'user' " +
            "             and roleUser.entity_id = #{login_Code} and role.id = #{role_id} " +
            "             and org.id = #{loginOrg_id}) role " +
            "         where  menu.status = '0' and roleMenu.status = '0' " +
            "                and menu.id = roleMenu.entity_id and roleMenu.type = 'menu' " +
            "                and roleMenu.Role_Id = role.id " +
            "                order by menu.tree_sorts , menu.tree_sort;")
    List<Menu> getMenusByLoginUserAndLoginOrg4Role(@Param("login_Code")String loginCode , @Param("loginOrg_id")String orgCode , @Param("role_id")String roleId);*/

    @Select("select c.id as id from SYS_ROlE_RELATION a , sys_role b , sys_menu c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            "and a.role_ID = b.id and a.entity_id = c.id and a.type = 'menu' " +
            "and b.id = #{id} " +
            "order by c.tree_sorts , c.tree_sort")
    List<String> getMenus4Id(Role role);

/*    @Select("select c.* from SYS_ROlE_RELATION a , sys_role b , sys_user c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            "and a.role_ID = b.id and a.entity_id = c.id and a.type = 'user' " +
            "and b.id = #{id} ")
    List<User> getUsers(Role role);*/

    @Select("select b.* from SYS_ROlE_RELATION a , sys_role b , sys_user c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            "and a.role_ID = b.id and a.entity_id = c.id and a.type = 'user' " +
            "and c.login_code = #{loginCode} " +
            "and b.role_code = #{superManagerRole}")
    Role isSystemRole(String loginCode , String superManagerRole);

    @Select("select c.id as id from SYS_ROlE_RELATION a , sys_role b , sys_user c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            "and a.role_ID = b.id and a.entity_id = c.id and a.type = 'user' " +
            "and b.id = #{id} ")
    List<String> getUsers4Id(Role role);

    @Override
    @Select("select distinct a.id as id , a.create_by as createBy , a.create_date as createDate , a.update_by as updateBy , a.update_date as updateDate , a.status as status , a.remarks as remarks , " +
            "a.role_code as roleCode , a.role_name as roleName , a.role_type as roleType ,  " +
            "a.role_sort as roleSort , a.is_sys as isSys , a.user_type as userType , a.data_scope as dataScope " +
            "from v_sys_roleAndOrg a " +
            "${ew.customSqlSegment} ")
    IPage<Role> selectPage(IPage<Role> page, @Param("ew")Wrapper<Role> queryWrapper);

}
